package com.xdh.zzqh.ui.dialog

import android.view.ViewGroup.LayoutParams
import android.widget.Button
import com.xdh.zzqh.R
import com.xdh.zzqh.ui.view.LabelTextInput
import com.xdh.zzqh.utils.onClick
import com.xdh.zzqh.utils.stringById
import kotlinx.coroutines.CompletableDeferred

class CreateUserDialog : DraggableWindow(800, LayoutParams.WRAP_CONTENT, R.layout.dialog_user_create) {

    private val tvName = view.findViewById<LabelTextInput>(R.id.id_user_lti_name)
    private val btnCreate = view.findViewById<Button>(R.id.id_user_btn_create2)

    suspend fun waitCreate(): String {

        var flag = false
        val complete = CompletableDeferred<String>()

        window.setOnDismissListener {
            if (flag) {
                return@setOnDismissListener
            }
            complete.completeExceptionally(IllegalStateException(stringById(R.string.qu_xiao_chuang_jian)))
        }

        btnCreate.onClick {
            flag = true
            val name = tvName.text()
            complete.complete(name)
            dismiss()
        }

        showCenter(-100)

        return complete.await()
    }
}